<template>
  <div class="wrapper">
    <!-- <el-card class="form-wrapper" shadow="never">
      <el-form class="form" :model="formData" inline>
        <el-form-item label="商家">
          <el-input v-model="formData.username" clearable @keyup.enter="resetPage" />
        </el-form-item>
        <el-form-item label="客户">
          <el-input v-model="formData.username" clearable @keyup.enter="resetPage" />
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="formData.username" clearable @keyup.enter="resetPage" />
        </el-form-item>
        <el-form-item label="业务员">
          <el-input v-model="formData.nickname" clearable @keyup.enter="resetPage" />
        </el-form-item>
        <el-form-item label="客服">
          <el-input v-model="formData.nickname" clearable @keyup.enter="resetPage" />
        </el-form-item>
        <el-form-item label="抽奖时间">
          <el-input v-model="formData.nickname" clearable @keyup.enter="resetPage" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card> -->
    <el-card v-loading="pager.loading" class="mt-4 !border-none" shadow="never">
      <el-button type="primary" size="small" plain @click="handleAdd">
        <template #icon>
          <icon name="el-icon-Plus" />
        </template>
        新增
      </el-button>

      <div class="mt-4">
        <el-table :data="pager.lists" size="large">
          <el-table-column label="序号" prop="id" min-width="60" />
          <el-table-column label="图标" min-width="100">
            <template #default="{ row }">
              <el-avatar :size="50" :src=row.img shape="square"></el-avatar>
            </template>
          </el-table-column>
          <el-table-column label="奖品名称" min-width="100" prop="name"></el-table-column>
          <el-table-column label="数量" min-width="100" prop="num"></el-table-column>
          <el-table-column label="状态" min-width="100" prop="status">
            <template #default="{ row }">
              <el-switch :model-value="row.status" :active-value="1" :inactive-value="0"
                @change="changeStatus($event, row.id)" />
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120" fixed="right">
            <template #default="{ row }">
              <el-button type="primary" link @click="handleEdit(row)">
                编辑
              </el-button>
              <el-button type="danger" link @click="handleDelete(row.id)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
  </div>
</template>

<script lang="ts" setup>
import { deleteApi, listsApi, statusApi } from '@/api/activity/pize'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import { nextTick, onMounted, reactive, ref, shallowRef } from 'vue'
import pagination from '@/components/pagination/index.vue'
const host = import.meta.env.VITE_APP_BASE_URL
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 表单数据
const formData = reactive({
  username: '',
  nickname: '',
  role: '',
})
const showEdit = ref(false)
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: listsApi,
  params: formData,
})

const changeStatus = async (active: any, id: number) => {
  await statusApi({
    id,
    status: active,
  })
  feedback.msgSuccess('修改成功')
  getLists()
}
const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.setFormData(data)
}

const handleDelete = async (id: number) => {
  await feedback.confirm('确定要删除？')
  await deleteApi({ id })
  feedback.msgSuccess('删除成功')
  getLists()
}

onMounted(() => {
  getLists()
})
</script>
<style lang="scss" scoped>
.text_hidden {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
}

.wrapper {
  .form-wrapper {
    padding-bottom: 0px;

    .form {
      // margin-bottom: 14px;
    }
  }
}
</style>
